﻿/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Javascript Utility Library
Author  : Pinal Bhatt
Version : 1.0
http://pbjsutils.codeplex.com/
Freeze Date: 12-Dec-2010
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/*************************************************
*   String Utilites                              *
**************************************************/
function stripHTML(originalStr) {

    stripHTML(originalStr, "");
}

function stripHTML(originalStr, replacerStr) {
    var regex = /<\S[^><]*>/g;
    return originalStr.replace(regex, replacerStr);
}

function isValidEmail(sText) {
    var regexEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
    return regexEmail.test(sText);
}

function isValidUrl(originalStr) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(originalStr);
}

function isEmpty(text) {
    var editorTextLength = text.replace(/\s+|\n+|\t+/g, "").length;
    return editorTextLength == 0;
}

function stripHTMLAndTrim(text) {
    var htmlStriper = /<(?:.|\s)*?>/g;
    text = text.replace(htmlStriper, " ");
    while (text.indexOf("  ") >= 0) {
        text = text.replace("  ", " ");
    }
    return text.replace(/^\s+|\s+$/g, "");
}

/* String Trim Utilites *********************/

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function () {
    return this.replace(/\s+$/, "");
}

/* String.Format Utilites *********************/
String.format = function (text) {

    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                arguments[token + 1]);
    }
    return text;
};

String.prototype.format = function () {
    var txt = this;
    for (var i = 0; i < arguments.length; i++) {
        var exp = new RegExp('\\{' + (i) + '\\}', 'gm');
        txt = txt.replace(exp, arguments[i]);
    }
    return txt;
}
/*************************************************
*   Cookie Utilities                             *
**************************************************/
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ')
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0)
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

/*************************************************
*   HTTP Request Utilites                        *
**************************************************/
function getHttpRequestObject() {

    var xmlhttp = false;

    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
/*************************************************
*   General JS Utils                             *
**************************************************/

function getElementValue(eid) {
    return document.getElementById(eid).value;
}

function setElementValue(eid, val) {
    document.getElementById(eid).value = val;
}

/*************************************************
*   Script Injection for JSON                    *
**************************************************/

function injectScript(url) {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    head.appendChild(script);
}

/*************************************************
*   Html Utilities                               *
**************************************************/

function getMetaContents(meta_name) {
    var my_arr = document.getElementsByTagName("META");
    for (var counter = 0; counter < my_arr.length; counter++) {
        if (my_arr[counter].name.toLowerCase() == meta_name.toLowerCase()) {
            return my_arr[counter].content;
        }
    }
    return "";
}

function getQueryStringValue(parameterName) {
    var objQRs = new Object();

    window.location.search.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"),
                function ($0, $1, $2, $3) { objQRs[$1] = $3; });

    return objQRs[parameterName]
}
/****************** Javascript Utility Library (Version 1.0) 01/Dec/2010 *****************/

